/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/* global BMap */

var cityName;
var map;
var myValue;
var deferred = $.Deferred();
$(function () {
    var myCity = new BMap.LocalCity();
    myCity.get(setCityName);
    deferred.then(initAutocomplete, function () {});
});
function initAutocomplete() {
console.info("initAutocomplete");

//    if ($("#bmap").length !== 0)
//    {
    map = new BMap.Map("bmap");
    var size = new BMap.Size(10, 20);
    var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT}); // 左上角，添加比例尺
    var top_left_navigation = new BMap.NavigationControl(); //左上角，添加默认缩放平移控件
    var cityListControl = new BMap.CityListControl({
        anchor: BMAP_ANCHOR_TOP_RIGHT,
        offset: size
                // 切换城市之间事件
                // onChangeBefore: function(){
                //    alert('before');
                // },
                // 切换城市之后事件
                // onChangeAfter:function(){
                //   alert('after');
                // }
    })

    //map.enableScrollWheelZoom(true);


    map.addControl(top_left_control);
    map.addControl(top_left_navigation);
    map.addControl(cityListControl);


    var geoc = new BMap.Geocoder();
    map.addEventListener("click", function (e) {
        console.info(" map.addEventListener function");
        var pt = e.point;
        geoc.getLocation(pt, function (rs) {
//			var addComp = rs.addressComponents;
//			alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
//		
//                alert(rs.surroundingPois[0].address);
//                var add;
//                if (rs.surroundingPois.length > 0)
//                {
//                    add = rs.surroundingPois[0].address;
//                    pt = rs.surroundingPois[0].point;
//                } else {
//                    add = rs.address;
//                }
            var add = rs.address;
            $(".address").val(add);
            map.clearOverlays(); //清除地图上所有覆盖物
            map.setCenter(pt);
            map.addOverlay(new BMap.Marker(pt)); //添加标注
            setLngAndLat(pt);
        });
    });
//
//
//    }
    if (map === undefined)
        map = cityName;
    $(".address").each(function () {
        console.info("$(.address");
        var ac = new BMap.Autocomplete(//建立一个自动完成的对象
                {"input": this
                    , "location": map
                });
        ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
            console.info("addEventListener function");
            var _value = e.item.value;
            myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
            $("#searchResultPanel").attr("address", myValue);
            if (map !== cityName)
                setPlace();
        });
    });


    if (caddress != "" && caddress != null) {
        $(".address").val(caddress);
        map.clearOverlays(); //清除地图上所有覆盖物
        var pt = new BMap.Point($("[id*='longitude']").val(), $("[id*='latitude']").val());
        map.setCenter(pt);
        map.addOverlay(new BMap.Marker(pt)); //添加标注
        //setLngAndLat(pt); 
map.centerAndZoom(pt, 13);
    } else {
        map.centerAndZoom(cityName, 13); // 初始化地图,设置城市和地图级别。
    }

}

function setCityName(result) {
    console.info("setCityName");
    cityName = result.name;
    deferred.resolve();
}



function setLngAndLat(point) {
    console.info("setLngAndLat");
    $("[id*='longitude']").val(point.lng);
//    alert( $("input[id*='latitude']").length);

    $("[id*='latitude']").val(point.lat);
}




function setPlace() {
    console.info("setplace");
    map.clearOverlays(); //清除地图上所有覆盖物
    function myFun() {
        var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
//        map.centerAndZoom(pp, 18);
        map.setCenter(pp);
        map.addOverlay(new BMap.Marker(pp)); //添加标注
        setLngAndLat(pp);
    }
    var local = new BMap.LocalSearch(map, {//智能搜索
        onSearchComplete: myFun
    });
    local.search(myValue);
}

